package com.lalameow.packet.packImpl;

import com.lalameow.packet.BasePack;
import com.lalameow.packet.enumtype.PackType;
import com.lalameow.serverlogingate.enetiy.PlayerInfo;
import com.lalameow.serverlogingate.service.PlayerInfoService;
import com.lalameow.serverlogingate.utiles.ApplicationContextUtil;
import com.lalameow.serverlogingate.utiles.BeanUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 陈刚
 * 日期: 2018/2/26
 * 时间: 9:52
 * 功能：请进行修改
 */
@Data
public class RegistPack extends PacketAbstract {
    private String playerName;
    private String userName;
    private String password;
    private String qq;
    private String tuijianren;
    private String code;
    private long timespan;
    private String registDate;
    private String mac;
    private String machineCode;

    @Override
    public BasePack revice(ChannelHandlerContext ctx) {
        PlayerInfoService playerInfoService=(PlayerInfoService) ApplicationContextUtil.getContext().getBean("playerInfoService");
        RegistRecivePack registRecivePack=new RegistRecivePack();
        BasePack basePack=new BasePack();
        basePack.setPackType(PackType.REGISTRECIVE);
        if(playerInfoService.hasPlayerName(this.playerName)){
            registRecivePack.setRegistOk(false);
            registRecivePack.setMsg("§c游戏角色名称已经被注册！");
            basePack.setBodyContent(registRecivePack.toJsonStr());
            return basePack;
        }
        if( playerInfoService.hasUserName(this.userName)){
            registRecivePack.setRegistOk(false);
            registRecivePack.setMsg("§c用户名已经被注册！");
            basePack.setBodyContent(registRecivePack.toJsonStr());
            return basePack;
        }
        PlayerInfo playerInfo=new PlayerInfo();
        this.userName=this.userName.trim();
        this.playerName=this.playerName.trim();
        this.password=this.password.trim();
        BeanUtil.beanCopy(this,playerInfo);

        playerInfo=playerInfoService.save(playerInfo);
        registRecivePack.setRegistOk(true);
        registRecivePack.setMsg(String.format("§c恭喜玩家§2【§l%s】§c注册成功！用户名为§2:【§l%s】，§c密码为:§4【§l%s】",this.playerName,this.userName,this.password));
        basePack.setBodyContent(registRecivePack.toJsonStr());
        return basePack;
    }
}
